package com.mt.baseinfo.until;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.engine.BeetlTemplateEngine;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class MybatisPlusFastAutoGenerator {

    private static String url = "jdbc:mysql://47.97.200.224:3306/resume";
    private static String userName = "root";
    private static String password = "Ma+1235678";
    private static String table = "resume";

    private static final DataSourceConfig.Builder DATA_SOURCE_CONFIG = new DataSourceConfig
            .Builder(url, userName, password);

    public static void main(String[] args) {

        generator();
    }

    public static void generator() {
        /*FastAutoGenerator.create(url, userName, password)
                .globalConfig(builder -> {
                    builder.author("mt") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir("D://"); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent("com.mt.baseinfo.mapper") // 设置父包名
                            .moduleName("system") // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.xml, "C:\\Users\\mt\\Desktop\\generate")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude("base_data_dict") // 设置需要生成的表名
                            .addTablePrefix("", ""); // 设置过滤表前缀
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();*/


        FastAutoGenerator fastAutoGenerator = FastAutoGenerator.create(DATA_SOURCE_CONFIG);
        fastAutoGenerator.globalConfig((scanner, builder) -> builder.author(scanner.apply("请输入作者名称？")).fileOverride());
        fastAutoGenerator.packageConfig((scanner, builder) -> builder.parent(scanner.apply("请输入包名？")));
        fastAutoGenerator.strategyConfig((scanner, builder) -> builder.addInclude(getTables(scanner.apply("请输入表名，多个英文逗号分隔？所有输入 all")))
                .controllerBuilder().enableRestStyle().enableHyphenStyle()
                .entityBuilder().enableLombok().addTableFills(
                        new Column("create_time", FieldFill.INSERT)
                ).build());
        fastAutoGenerator.templateEngine(new BeetlTemplateEngine())
                .templateEngine(new FreemarkerTemplateEngine()).execute();// 全局配置
        // 包配置
        // 策略配置
                    /*
                    模板引擎配置，默认 Velocity 可选模板引擎 Beetl 或 Freemarker
                   .templateEngine(new BeetlTemplateEngine())
                   .templateEngine(new FreemarkerTemplateEngine())
                 */


    }

    // 处理 all 情况
    protected static List<String> getTables(String tables) {
        return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }
}
